package com.campus.help.controller;

import com.campus.help.common.Result;
import com.campus.help.common.PageResult;
import com.campus.help.dto.MessageDTO;
import com.campus.help.service.MessageService;
import com.campus.help.vo.MessageVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 消息控制器
 */
@RestController
@RequestMapping("/messages")
@RequiredArgsConstructor
public class MessageController {
    
    private final MessageService messageService;
    
    /**
     * 分页查询用户消息
     */
    @GetMapping
    public Result<PageResult<MessageVO>> getUserMessages(
            @RequestParam Long userId,
            @RequestParam(defaultValue = "all") String type,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {
        
        PageResult<MessageVO> result = messageService.getUserMessages(userId, type, page, size);
        return Result.success(result);
    }
    
    /**
     * 获取用户未读消息统计
     */
    @GetMapping("/unread-stats")
    public Result<Map<String, Integer>> getUnreadStats(@RequestParam Long userId) {
        Map<String, Integer> stats = messageService.getUnreadMessageStats(userId);
        return Result.success(stats);
    }
    
    /**
     * 获取消息详情
     */
    @GetMapping("/{messageId}")
    public Result<MessageVO> getMessageDetail(
            @PathVariable Long messageId,
            @RequestParam Long userId) {
        
        MessageVO message = messageService.getMessageDetail(messageId, userId);
        if (message == null) {
            return Result.error("消息不存在");
        }
        return Result.success(message);
    }
    
    /**
     * 标记消息为已读
     */
    @PutMapping("/{messageId}/read")
    public Result<Void> markMessageAsRead(
            @PathVariable Long messageId,
            @RequestParam Long userId) {
        
        boolean success = messageService.markMessageAsRead(userId, messageId);
        return success ? Result.success() : Result.error("操作失败");
    }
    
    /**
     * 批量标记消息为已读
     */
    @PutMapping("/batch-read")
    public Result<Void> markMessagesAsRead(
            @RequestParam Long userId,
            @RequestBody List<Long> messageIds) {
        
        boolean success = messageService.markMessagesAsRead(userId, messageIds);
        return success ? Result.success() : Result.error("操作失败");
    }
    
    /**
     * 标记所有消息为已读
     */
    @PutMapping("/read-all")
    public Result<Void> markAllMessagesAsRead(
            @RequestParam Long userId,
            @RequestParam(defaultValue = "all") String type) {
        
        boolean success = messageService.markAllMessagesAsRead(userId, type);
        return success ? Result.success() : Result.error("操作失败");
    }
    
    /**
     * 删除消息
     */
    @DeleteMapping
    public Result<Void> deleteMessages(
            @RequestParam Long userId,
            @RequestBody List<Long> messageIds) {
        
        boolean success = messageService.deleteMessages(userId, messageIds);
        return success ? Result.success() : Result.error("操作失败");
    }
    
    /**
     * 发送消息
     */
    @PostMapping
    public Result<Void> sendMessage(
            @Valid @RequestBody MessageDTO messageDTO,
            @RequestParam Long senderId,
            @RequestParam String senderName) {
        
        boolean success = messageService.sendMessage(messageDTO, senderId, senderName);
        return success ? Result.success() : Result.error("发送失败");
    }
}
